package com.kid.keep.pattern;

import com.kid.keep.dao.item.CheckItemStandardRepository;
import com.kid.keep.enums.BizExceptionEnum;
import com.kid.keep.enums.ItemEnum;
import com.kid.keep.exception.BizException;
import com.kid.keep.po.CheckItemStandard;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description 报告等级
 * @Author JOHN
 * @Create 2020-01-03
 * @Since 1.0.0
 */

public class ReportGradeHandler {

    private ReportGradePattern first;

    public ReportGradeHandler(CheckItemStandardRepository checkItemStandardRepository) {
        List<CheckItemStandard> standardList = checkItemStandardRepository.findAllByItemId(ItemEnum.REPORT.getValue());
        setReportGradeHandler(standardList);
    }

    /**
     * 设置测试标准
     *
     * @param standardList
     */
    private void setReportGradeHandler(List<CheckItemStandard> standardList) {
        if (CollectionUtils.isEmpty(standardList)) {
            throw BizException.getInstance(BizExceptionEnum.NO_GRADE);
        }
        for (CheckItemStandard checkItemStandard : standardList) {
            if (Objects.isNull(first)) {
                first = new ReportGradePattern(checkItemStandard);
                continue;
            }
            getNext(first, checkItemStandard);
        }
    }

    /**
     * 设置标准
     *
     * @param reportGradePattern
     * @param checkItemStandard
     */
    private void getNext(AbstractReportGradePattern reportGradePattern, CheckItemStandard checkItemStandard) {
        if (Objects.nonNull(reportGradePattern.getNext())) {
            getNext(reportGradePattern.getNext(), checkItemStandard);
        } else {
            reportGradePattern.setNext(new ReportGradePattern(checkItemStandard));
        }
    }

    /**
     * 获取等级
     *
     * @param grade
     * @return
     */
    public CheckItemStandard getGrade(Integer grade) {
        return this.first.execute(grade);
    }
}
